package com.example.dockerdemo.config;

import com.example.dockerdemo.entity.User;
import com.example.dockerdemo.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthenticatingRealm {
    @Autowired
    private UserService userService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //通过数据库查询获认证信息
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        User user = userService.selectByUsername(username);
        if (user==null) {
            throw new AuthenticationException("用户不存在");
        }
        String password = user.getPassword();
        return new SimpleAuthenticationInfo(username,password,getName());
    }
}
